package com.syp.lease.web.app.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.syp.lease.web.app.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private Client client;

    /**
     * 发送验证码短信到指定电话号码
     *
     * @param phone      接收短信的电话号码
     * @param verifyCode 验证码
     */
    @Override
    public void sendCode(String phone, String verifyCode) {
        // 创建发送短信请求对象
        SendSmsRequest smsRequest = new SendSmsRequest();

        // 设置短信签名名称
        smsRequest.setSignName("阿里云短信测试");

        // 设置短信模板代码
        smsRequest.setTemplateCode("SMS_154950909");

        // 设置接收短信的电话号码
        smsRequest.setPhoneNumbers(phone);

        // 设置短信模板参数，此处为验证码
        smsRequest.setTemplateParam("{\"code\":\"" + verifyCode + "\"}");

        // 尝试发送短信，如果发送失败则抛出运行时异常
        try {
            client.sendSms(smsRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
